﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Commons.Entity;
using Commons.Entity.CommonEnums;
using System.Transactions;
using Common.Background.Data;
using EF.Commons.Background.Entity;

namespace Common.Background.Service
{
    #region  部门设置
    public partial class SystemServices
    {
        #region 部门列表
        public List<WebDepartments> WebDepartmentsList(WebDepartments WebDepartments, VPredication vp)
        {
            List<WebDepartments> list = base.iWebDepartmentsRepository
                                            .GetModel()
                                            .Where(i => i.Status != (int)Status.Deleted)
                                            .OrderBy(item => item.SortNumber)
                                            .ToList();
            return list;
        }
        #endregion

        #region 更新部门
        public void SetWebDepartments(WebDepartments WebDepartments)
        {
            WebDepartments.UpdateDate = DateTime.Now;
            base.iWebDepartmentsRepository.Update(WebDepartments);
        }
        #endregion

        #region 添加部门
        public void AddWebDepartments(WebDepartments WebDepartments)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    WebDepartments.DepartmentID = new PKIDTableRepository().GetNewPKID(PKIDTable.WebDepartments);
                    WebDepartments.UpdateDate = DateTime.Now;
                    WebDepartments.Operator = string.Empty;
                    base.iWebDepartmentsRepository.Insert(WebDepartments);
                    trans.Complete();
                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {
                    trans.Dispose();
                }
            }

        }
        #endregion

        #region 删除部门
        public void DeleteWebDepartments(WebDepartments WebDepartments)
        {
            WebDepartments entity = base.iWebDepartmentsRepository.Find(WebDepartments.DepartmentID);
            base.iWebDepartmentsRepository.Delete(GetSubDeptList(entity));
        }
        #endregion

        #region 递归得到部门列表
        List<WebDepartments> deptList;

        /// <summary>
        /// 得到当前部门的所有子部门列表
        /// </summary>
        /// <param name="WebDepartments"></param>
        /// <returns></returns>
        public List<WebDepartments> GetSubDeptList(WebDepartments WebDepartments)
        {
            deptList = new List<WebDepartments>();
            deptList.Add(WebDepartments);
            GetSublDept(WebDepartments);
            return deptList;
        }
        /// <summary>
        /// 根据父对象，找到子孙树
        /// </summary>
        /// <param name="father">父对象</param>
        void GetSublDept(WebDepartments father)
        {
            father.Sons = iWebDepartmentsRepository.GetModel().Where(item => item.ParentID.Equals(father.DepartmentID)).ToList();
            father.Sons.ForEach(item =>
            {
                deptList.Add(item);
                item.Father = father;
                GetSublDept(item);
            });

        }
        #endregion
    }
    #endregion
}